//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace HappySprite.Graphics
{
    public class Quad : RenderItem
    {
        Vector2 halfExtents;
        ImageMap imageMap;
        Color tint = Color.White;

        static QuadEffect quadEffect = new QuadEffect();

        public static QuadEffect QuadEffect
        {
            get { return quadEffect; }
        }

        public Vector2 Size
        {
            get { return halfExtents * 2f; }
            set { halfExtents = value / 2f; }
        }

        public float Height
        {
            get { return halfExtents.Y * 2f; }
            set { halfExtents.Y = value / 2f; }
        }

        public float Width
        {
            get { return halfExtents.X * 2f; }
            set { halfExtents.X = value / 2f; }
        }

        public Color Tint
        {
            get { return tint; }
            set { tint = value; }
        }

        public ImageMap ImageMap
        {
            get { return imageMap; }
            set { imageMap = value; }
        }

        public override void Draw(Renderer renderer)
        {
            // Nothing to render
            if (ImageMap == null || ImageMap.Texture == null)
                return;

            quadEffect.View = renderer.View;
            quadEffect.Projection = renderer.Projection;
            quadEffect.World = GetRenderWorld(renderer.SceneItem.World);
            quadEffect.Tint = Tint;
            quadEffect.ImageMap = new Vector4(imageMap.UpperLeft, imageMap.LowerRight.X, imageMap.LowerRight.Y);
            quadEffect.Texture = imageMap.Texture;

            quadEffect.Render();
        }

        Matrix GetRenderWorld(Transform3D item)
        {
            Matrix m = Matrix.CreateScale(halfExtents.X, halfExtents.Y, 1f);
            item.Append(ref m);

            return m;
        }
    }
}
